﻿using System.Net.Http;
using CkSoftware.GroupMe.Sdk.Core;
using CkSoftware.GroupMe.Sdk.Core.Dao;
using CkSoftware.GroupMe.Sdk.Groups.Dao;

namespace CkSoftware.GroupMe.Sdk.Groups.Commands
{
	/// <summary>
	/// Create group command (GroupMe documentation: <see href="https://dev.groupme.com/docs/v3#groups_create" />).
	/// </summary>
	public class CreateGroupCommand : IWebApiCommand<GroupMeApiResponse<GroupData>>
	{
		private readonly CreateGroupParameters _parameters;

		/// <summary>
		/// Instantiates a new <see cref="CreateGroupCommand" /> with the given groupName.
		/// </summary>
		/// <param name="groupName">The name of the group to create.</param>
		public CreateGroupCommand(string groupName)
		{
			_parameters = new CreateGroupParameters {Name = groupName};
		}

		/// <summary>
		/// Defines if the command is executed once.
		/// </summary>
		public bool CommandExecuted { get; set; }

		/// <summary>
		/// The name of the command to be executed.
		/// </summary>
		public string CommandName
		{
			get { return "groups"; }
		}

		/// <summary>
		/// The http method to execute this command.
		/// </summary>
		public HttpMethod HttpMethod
		{
			get { return HttpMethod.Post; }
		}

		/// <summary>
		/// The parameters which should be used on command execution.
		/// </summary>
		public IWebApiCommandParameters Parameters
		{
			get { return _parameters; }
		}

		/// <summary>
		/// Defines that the group is open for sharing / free to join.
		/// </summary>
		/// <returns>The extended <see cref="CreateGroupCommand" />.</returns>
		public CreateGroupCommand OpenForSharing()
		{
			_parameters.IsOpenForSharing = true;
			return this;
		}

		/// <summary>
		/// Adds the given description to the new group.
		/// </summary>
		/// <param name="description">The description of the new group.</param>
		/// <returns>The extended <see cref="CreateGroupCommand" />.</returns>
		public CreateGroupCommand WithDescription(string description)
		{
			_parameters.Description = description;
			return this;
		}

		/// <summary>
		/// Defines a image for the new group.
		/// </summary>
		/// <param name="imageUrl">The image url of the new group. Must be a GroupMe ImagingService image-url according to the GroupMe API documentation.</param>
		/// <returns>The extended <see cref="CreateGroupCommand" />.</returns>
		public CreateGroupCommand WithImage(string imageUrl)
		{
			_parameters.ImageUrl = imageUrl;
			return this;
		}

		/// <summary>
		/// Defines a image for the new group.
		/// </summary>
		/// <param name="imageData">The image data for the group-image.</param>
		/// <returns>The extended <see cref="CreateGroupCommand" />.</returns>
		public CreateGroupCommand WithImage(PictureData imageData)
		{
			_parameters.GroupImage = imageData;
			return this;
		}
	}
}